package cn.itcast.com.Thread;

import lombok.SneakyThrows;

import java.lang.management.LockInfo;

public class deadlock {
    //该🔒对象用对共享变量进行线程同步
    public static final Object lockA = new Object();
    //该🔒对象用来对打印机的访问进行线程同步
    public static final Object lockB = new Object();
    public static final Object Alllock = new Object();
    public static void main(String[] args) {
        ABThread abThread = new ABThread();
        BAThread baThread = new BAThread();
        abThread.start();
        baThread.start();
    }
}

class ABThread extends Thread{
    @SneakyThrows
    public void run(){
        synchronized (deadlock.Alllock) {
            //访问共享变量，进行计算，得到一些中间结果
            synchronized (deadlock.lockA){
                Thread.sleep(100);
                synchronized (deadlock.lockB){
                    //将计算的结果发送给打印机开始打印
                }
            }
        }
    }
}

class BAThread extends Thread{
    @SneakyThrows
    public void run(){
        synchronized (deadlock.Alllock) {
            synchronized (deadlock.lockB){
                Thread.sleep(100);
                synchronized (deadlock.lockA){

                }
            }
        }
    }
}